package com.hk.aefz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.aefz.mapper.AccountMapper;
import com.hk.aefz.mapper.UserInfoMapper;
import com.hk.aefz.po.Account;
import com.hk.aefz.po.UserInfo;
import com.hk.aefz.service.AccountService;
import com.hk.aefz.utils.DateUtils;
import com.hk.aefz.vo.ResultMsg;
import org.apache.shiro.crypto.hash.Hash;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * <p>
 * 账户表 服务实现类
 * </p>
 *
 * @author 阿尔法哲
 * @since 2020-07-21
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 注册账户及用户信息及关联默认角色
     * @param aaccount
     * @param aemail
     * @param apassword
     * @return
     */
    @Override
    @Transactional
    public ResultMsg saveAccount(String aaccount, String aemail, String apassword,String yzm,String yzmm) {
        ResultMsg resultMsg = new ResultMsg();
        if (yzm == null || !yzm.equals(yzmm.toLowerCase())) {
            resultMsg.setState(400);
            resultMsg.setMsg("验证码输入错误！");
            return resultMsg;
        }else {
            Account account = new Account(aaccount, aemail, apassword);
            //        新增账户
            accountMapper.insert(account);
//          新增用户信息 封装
            UserInfo userInfo = new UserInfo();
//            默认昵称随机uuid
            UUID uuid = UUID.randomUUID();
            String[] split = uuid.toString().split("-");
            userInfo.setUname(split[0]);
            userInfo.setUemail(aemail);
            userInfo.setAid(account.getAid());
            userInfoMapper.insert(userInfo);
//            关联默认角色
            String gmt = new DateUtils().dateString(); //当前时间
            accountMapper.accountRoleSave(account.getAid(),3,gmt,gmt);
            resultMsg.setState(201);
            resultMsg.setMsg("注册成功,请登录！");
            return resultMsg;

        }

    }

    /**
     * 通过账户id查询用户角色
     * @param aid
     * @return
     */
    @Override
    public Set<String> selectByAid(Integer aid) {
        Set<String> roles = accountMapper.selectByAid(aid);
        roles.forEach(System.out :: println);
        return roles;
    }

}
